/* Kanvas Paint and Drawing Tool.
 * Copyright (C) 2011 Darryl Wallace
 * wallacdj@gmail.com / darryl@wallynet.ca
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "MouseToolsWidget.h"
#include "PluginManager.h"
#include "../PluginInterface/ITool.h"

#define MOUSE_LEAVE_OPACITY 0.5

MouseToolsWidget::MouseToolsWidget(PluginManager *pluginManager, QWidget *parent)
    : QDockWidget(tr("Tools"),parent),
      _currentTool(NULL)
{
    setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
    foreach (ITool *t, pluginManager->tools())
    {
        _tools.append(t);
    }

    setupUi();
    connect(this, SIGNAL(topLevelChanged(bool)),
            this, SLOT(getDockWidgetFloating(bool)));

    _opacityAnimation.setTargetObject(this);
    _opacityAnimation.setDuration(100);
    _opacityAnimation.setPropertyName("windowOpacity");
}

MouseToolsWidget::~MouseToolsWidget()
{

}

void MouseToolsWidget::setupUi()
{
    _internalWidget = new QWidget();
    QVBoxLayout *vlayout = new QVBoxLayout();
    _layout = new FlowLayout();
    QButtonGroup * buttonGroup = new QButtonGroup(this);
    buttonGroup->setExclusive(true);
    for (int i=0; i<_tools.size();i++)
    {
        ITool *t = _tools.at(i);
        QPushButton *button = new QPushButton();
        QIcon icon = t->toolIcon();
        button->setIcon(icon);
        button->setToolTip(t->toolName());
        button->setFlat(true);
        button->setCheckable(true);
        buttonGroup->addButton(button,i);
        _toolButtons.append(button);
    }

    connect(buttonGroup, SIGNAL(buttonClicked(int)),this, SLOT(getToolClicked(int)));

    for (int i=0; i<_toolButtons.size();i++)
    {
        _layout->addWidget(_toolButtons.at(i));//,row,col);
    }
    vlayout->addLayout(_layout);
    vlayout->addStretch();
    _internalWidget->setLayout(vlayout);
    setWidget(_internalWidget);
}

ITool * MouseToolsWidget::currentTool() const
{
    return _currentTool;
}

void MouseToolsWidget::getToolClicked(int id)
{
        _currentTool = _tools.at(id);
        emit toolSelectionChanged();
}

void MouseToolsWidget::getDockWidgetFloating(bool floating)
{
    _floating=floating;
    if (!_floating)
        setWindowOpacity(1.0);  //ensure window opacity is 1

}

void MouseToolsWidget::enterEvent(QEvent *e)
{

    if (_floating)
    {
        _opacityAnimation.setEndValue(1.0);
        _opacityAnimation.start();
    }

    QDockWidget::enterEvent(e);

}

void MouseToolsWidget::leaveEvent(QEvent *e)
{

    if (_floating)
    {
        _opacityAnimation.setEndValue(MOUSE_LEAVE_OPACITY);
        _opacityAnimation.start();
    }
    QDockWidget::leaveEvent(e);
}
